package top.cross.webapp.common.base;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import top.cross.webapp.common.R;
import top.cross.webapp.common.view.ZProgressDialog;

public abstract class BaseStatusBarActivity extends AppCompatActivity {

    View titleInclude;
    ZProgressDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        titleInclude = findViewById(R.id.title);
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        initIntentData();
        initTitleLayout();
        ViewStub stub = findViewById(R.id.content);
        stub.setLayoutResource(getLayoutRes());
        stub.inflate();
        initView();
    }

    public void setTitleVisibility(boolean visibility) {
        if (visibility) {
            titleInclude.setVisibility(View.VISIBLE);
        } else {
            titleInclude.setVisibility(View.GONE);
        }
    }

    /**
     * 获取展示内容布局ID R.layout.xxx
     *
     * @return
     */
    @LayoutRes
    public abstract int getLayoutRes();

    /**
     * 初始化标题栏相关操作
     */
    public abstract void initTitleLayout();

    /**
     * 获取传入的Intent数据
     */
    public abstract void initIntentData();

    /**
     * 初始化界面组件
     */
    public abstract void initView();

    public void settitle(String title) {
        ((TextView) findViewById(R.id.tv_title)).setText(title);
    }

    public void showWaitDialog() {
        if (dialog == null) {
            dialog = ZProgressDialog.createProgressDialog(this, "加载中");
        }
        dialog.show();
    }

    /**
     * 显示弹窗
     *
     * @param msg                    内容
     * @param cancelable             取消
     * @param canceledOnTouchOutside 外部取消
     */
    public void showWaitDialog(String msg, boolean cancelable, boolean canceledOnTouchOutside) {
        if (null == dialog) {
            dialog = ZProgressDialog.createProgressDialog(this, msg);
        }
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
        dialog.setCancelable(cancelable);
        dialog.show();
    }

    public void dismissWaitDialog() {
        dialog.dismiss();
    }

    @Override
    public void onBackPressed() {
        try {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(
                            getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            // TODO: handle exception
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
